package com.collectplus.express.wxapi;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.collectplus.express.logic.AppConfig;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXMiniProgramObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import droid.frame.utils.android.Alert;
import droid.frame.utils.lang.Str;

import static com.tencent.mm.opensdk.modelmsg.WXMiniProgramObject.MINIPROGRAM_TYPE_PREVIEW;
import static com.tencent.mm.opensdk.modelmsg.WXMiniProgramObject.MINIPTOGRAM_TYPE_RELEASE;

/**
 * 微信分享
 *
 * @author coffee <br>
 *         2016-10-13下午4:54:11
 */
public final class WXshare {
    private IWXAPI api;
    private Context context;

    /**
     * @param activity
     * @return
     */
    public WXshare(final Activity activity) {
        context = activity;
        api = WXAPIFactory.createWXAPI(activity, Constants.APP_ID, false);
    }

    /**
     * 微信信息
     */
    public void sendChatMessage(String title, String description) {
        Bitmap image = BitmapFactory.decodeResource(context.getResources(), com.collectplus.express.R.drawable.ic_launcher);
        String url = "";
        sendMessage(0, title, description, image, url);
    }

    /**
     * 微信朋友圈
     */
    // public void sendTimelines(String title, String description) {
    // Bitmap image = BitmapFactory.decodeResource(context.getResources(), com.collectplus.express.R.drawable.ic_launcher);
    // String url = "http://www.baidu.com";
    // sendMessage(1, title, description, image, url);
    // }

    /**
     * 分享微信内容的
     *
     * @param type        0 为分享朋友 1为分享朋友圈
     * @param title       标题
     * @param description 宣传内容
     * @param icon        宣传图标
     * @param url         点击发往路径
     */
    public void sendMessage(int type, String title, String description, Bitmap icon, String url) {
        api.registerApp(Constants.APP_ID);
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = url;
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = title;
        msg.description = description;
        if (icon != null) {
            msg.setThumbImage(icon);
        }
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = type;
        api.sendReq(req);
        // Log.d("wx:appid", Constants.APP_ID);
        // Log.d("wx:share", type + " " + title + " " + description + " " + icon + " " + url);
    }

    /**
     * 分享
     *
     * @param type           0 好友  1朋友圈
     * @param wxMiniUserName 小程序用户名
     * @param wxMiniPath     小程序路径
     */
    public void shareWX(int type, String title, String description, Bitmap icon, String url, String wxMiniUserName, String wxMiniPath) {
        //调用api接口发送数据到微信
        if (api.isWXAppInstalled() == false) {
            Alert.toast("未安装微信");
            return;
        }
        WXMediaMessage msg = null;
        //不需要分享给小程序
        if (Str.isEmpty(wxMiniUserName) || Str.isEmpty(wxMiniPath)) {
            WXWebpageObject webpageObject = new WXWebpageObject();
            webpageObject.webpageUrl = url;
            msg = new WXMediaMessage(webpageObject);
        } else {
            WXMiniProgramObject wxMiniProgramObject = new WXMiniProgramObject();
            //低版本微信
            wxMiniProgramObject.webpageUrl = url;
            //高版本
            wxMiniProgramObject.userName = wxMiniUserName;
            wxMiniProgramObject.path = wxMiniPath;
            if (AppConfig.isTest) {
                wxMiniProgramObject.miniprogramType = MINIPROGRAM_TYPE_PREVIEW;
            } else {
                wxMiniProgramObject.miniprogramType = MINIPTOGRAM_TYPE_RELEASE;
            }
            msg = new WXMediaMessage(wxMiniProgramObject);
        }
        msg.title = title;
        msg.description = description;
        if (icon != null) {
            msg.setThumbImage(icon);
        }
        //下面的方法在小米手机中有问题
        //        msg.thumbData = BitmapUtils.bmpToByteArray(icon, true);
        //构造一个req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction("com.collectplus.express");//transaction字段用于唯一表示一个请求
        req.message = msg;
        req.scene = type == 0 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
        api.sendReq(req);
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

}

